package com.hl.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.Consult;
import com.hl.sfzy.entity.ConsultField;
import com.hl.sfzy.exception.AppException;
import com.hl.sfzy.mapper.ConsultFieldMapper;
import com.hl.sfzy.service.ConsultFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ConsultFieldServiceImpl
 * @Description WHATETODO
 * @Author Wxx
 * @Date 2024年01月15日 20:22
 * @Version 1.0
 */
@Service
public class ConsultFieldServiceImpl implements ConsultFieldService {
    @Autowired
    private ConsultFieldMapper consultFieldMapper;

    @Override
    public void add(ConsultField entity) throws Exception {
        ConsultField consultField = selectByName(entity.getField());
        if (consultField == null) {
            consultFieldMapper.insert(entity);
        } else {
            throw new AppException("用户名已存在!");
        }
    }

    @Override
    public void edit(ConsultField entity) {
        consultFieldMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        consultFieldMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        consultFieldMapper.deleteBatch(ids);
    }

    @Override
    public List<ConsultField> find(ConsultField entity) {
        return consultFieldMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, ConsultField entity) {
        //分页业务实现
        PageHelper.startPage(pageNum, pageSize);

        List<ConsultField> list = consultFieldMapper.select(entity); // 当前页数据

        PageInfo<ConsultField> pageInfo = new PageInfo<>(list); // 分页信息对象

        /*
         * 把前台需要的数据反馈
         * 1、当前页面数据（数据列表）
         * 2、此次查询的总记录数
         */
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getTotal());

        return pageMap;
    }

    @Override
    public ConsultField findById(Integer id) {
        return consultFieldMapper.selectById(id);
    }


    @Override
    public ConsultField selectByName(String name) {
        return consultFieldMapper.selectByName(name);
    }
}